#!/usr/bin/env python

"""%prog [options]

The duetopia-devel program helps with various aspects of development, mostly
related to working on several packages simultaneously. For more information
see the file README.devel in the top-level source directory."""

from optparse import OptionParser
import sys
import os
from os import environ
root_dir = os.path.dirname(os.path.realpath(__file__))

def package_dirs():
    def _find_modules():
        for root, children, files in os.walk(root_dir, topdown=True):
            if os.path.basename(root)[0] == ".":
                for k in range(len(children)):
                    children.pop()
                continue
            if "setup.py" in files:
                yield root
                for k in range(len(children)):
                    children.pop()
    return list(_find_modules())

def pythonpath():
    return ":".join(package_dirs())

def dist():
    if "DUETOPIA_VERSION" not in os.environ:
        os.environ["DUETOPIA_VERSION"] = version()
    dist_dir = os.path.join(root_dir, "dist", os.environ["DUETOPIA_VERSION"])
    try: os.stat(dist_dir)
    except OSError: os.makedirs(dist_dir)
    for pkg in package_dirs():
        command = """
            cd '%s'; rm -rf dist
            python setup.py sdist
        """ % pkg
        os.system(command)
        pkg_dist = os.path.join(pkg, "dist")
        for root, subdirs, files in os.walk(pkg_dist):
            for f in files:
                orig_path = os.path.join(root, f)
                new_path = os.path.join(dist_dir, f)
                os.link(orig_path, new_path)
                os.unlink(orig_path)

def install():
    #if "DUETOPIA_VERSION" not in os.environ:
    #    print "looking up version"
    #    os.environ["DUETOPIA_VERSION"] = version()
    for pkg in package_dirs():
        command = """
               cd '%s'; rm -rf dist
               python setup.py install
               """ % pkg
        os.system(command)

def version():
    sys.path = pythonpath().split(":") + sys.path
    from duetopia.utils.svn import version as _v
    return _v()

if __name__ == '__main__':
    op = OptionParser(usage = __doc__, epilog="At least one option must be specified.")
    op.add_option("-d", "--dist",
            dest="dist",
            action="store_true",
            default=False,
                    help="Make a distribution",
    )
    op.add_option("-i", "--install",                                                                dest="install",                                                                    action="store_true",                                                            default=False,                                                                  help="Install systemwide",                                  )    
    op.add_option("-p", "--python-path", 
            dest="pythonpath",
            action="store_true",
            default=False,
                    help="Print a PYTHONPATH for in-place development",
    )
    op.add_option("-v", "--version",
            dest="version",
            action="store_true",
            default=False,
            help="Print current working SVN version",
    )
    (options, args) = op.parse_args()
    if options.dist:
        dist()
        sys.exit(0)
    if options.pythonpath:
        print pythonpath()
        sys.exit(0)
    if options.version:
        print version()
        sys.exit(0)
    if options.install:
        print install()
        sys.exit(0)
    op.print_help()
    sys.exit(1)
